<template>
  <form @submit.prevent="handleSubmit" class="auth-form">
    <div class="input-group">
      <div class="input-wrapper">
        <input
          v-model="form.username"
          type="text"
          placeholder="用户名"
          class="form-input"
          autocomplete="username"
          required
        />
        <span class="input-icon">👤</span>
      </div>
    </div>

    <div class="input-group">
      <div class="input-wrapper">
        <input
          v-model="form.password"
          :type="showPassword ? 'text' : 'password'"
          placeholder="密码"
          class="form-input"
          autocomplete="current-password"
          required
        />
        <button
          type="button"
          class="password-toggle"
          @click="togglePassword"
        >
          {{ showPassword ? '🙈' : '👁️' }}
        </button>
      </div>
    </div>

    <button 
      type="submit" 
      :disabled="loading"
      class="submit-btn"
    >
      <span v-if="loading" class="loading-spinner"></span>
      {{ loading ? '登录中...' : '登录' }}
    </button>

    <!-- Test Account Info -->
    <div class="test-info">
      <p class="test-title">测试账号</p>
      <p class="test-credentials">
        <span class="credential">admin</span> / <span class="credential">123456</span>
      </p>
    </div>
  </form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

interface Props {
  loading?: boolean
}

interface Emits {
  (e: 'submit', data: { username: string; password: string }): void
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

const emit = defineEmits<Emits>()

const showPassword = ref(false)
const form = reactive({
  username: '',
  password: ''
})

const togglePassword = () => {
  showPassword.value = !showPassword.value
}

const validateForm = () => {
  if (!form.username.trim()) {
    ElMessage.warning('请输入用户名')
    return false
  }
  if (!form.password) {
    ElMessage.warning('请输入密码')
    return false
  }
  return true
}

const handleSubmit = () => {
  if (!validateForm()) return
  
  emit('submit', {
    username: form.username,
    password: form.password
  })
}

// Expose methods for parent component
defineExpose({
  setUsername: (username: string) => {
    form.username = username
  },
  clearForm: () => {
    form.username = ''
    form.password = ''
  }
})
</script>

<style scoped>
/* Form Styles */
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  position: relative;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.form-input {
  width: 100%;
  padding: 16px 20px;
  padding-right: 50px;
  border: 2px solid #e2e8f0;
  border-radius: 16px;
  font-size: 16px;
  color: #1e293b;
  background: white;
  transition: all 0.2s ease;
  outline: none;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input::placeholder {
  color: #94a3b8;
}

.input-icon {
  position: absolute;
  right: 16px;
  font-size: 1.2rem;
  color: #94a3b8;
  pointer-events: none;
}

.password-toggle {
  position: absolute;
  right: 16px;
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}

.password-toggle:hover {
  background-color: #f1f5f9;
}

/* Submit Button */
.submit-btn {
  width: 100%;
  padding: 16px 24px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.submit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Test Account Info */
.test-info {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 16px;
  text-align: center;
  margin-top: 20px;
}

.test-title {
  font-size: 14px;
  font-weight: 600;
  color: #475569;
  margin: 0 0 8px 0;
}

.test-credentials {
  font-size: 14px;
  color: #64748b;
  margin: 0;
}

.credential {
  background: #667eea;
  color: white;
  padding: 2px 8px;
  border-radius: 6px;
  font-weight: 500;
  font-family: 'Monaco', 'Menlo', monospace;
}

/* Responsive Design */
@media (max-width: 640px) {
  .form-input {
    padding: 14px 18px;
    padding-right: 45px;
    font-size: 15px;
  }
  
  .submit-btn {
    padding: 14px 20px;
    font-size: 15px;
  }
}
</style>
